<?php
include_once('baseModel.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/smartrfid/model/rfidInfo.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/smartrfid/model/itemInfo.php');

class RepairHistory extends BaseModel {
  public $_table="repairHistory";

  public $id;
  public $timestamp;
  public $description;
  public $item_id;

  
  function loadByItemId($id) {
    $query = "SELECT * FROM " . $this->_table . " WHERE item_id = " . $id;
    $results = DBHelper::executeQuery($query);
    if(!$results) {
      return;
    }
    // there might be more than one results
    $histories = array();
    
    foreach($results as $result) {
      $this->populateModel($result);
      array_push($histories, clone $this);
    }
    return $histories;
  }
  
  function loadByRfid($rfid) {
    $rfidInfo = new RfidInfo();
    $rfidInfo->load($rfid);
    
    $itemInfo = new ItemInfo();
    $itemInfo->load($rfidInfo->itemInfo_id);

    $histories = $this->loadByItemId($itemInfo->item_id);
    return $histories;
  }

  function insert() {
    $query = "INSERT INTO `" . $this->_table ."` ";
    $query .= "(`timestamp`, `description`, `item_id`) ";
    $query .= "VALUES (NOW(), \"" .addslashes($this->description)."\", {$this->item_id})";
    $success = DBHelper::updateQuery($query);
    return $success;
  }
  
}
